<?php

require_once 'Zend/View/Helper/Abstract.php';

class Core_View_Helper_Controls_GetXmlDeclaration extends Zend_View_Helper_Abstract
{
    protected $exclusion = array();

    /**
     * Return a string with the XML declaration if required
     *
     * Return a string with the XML declaration if required
     * If option 'browser' is used, it is decided by the browser's HTTP_USER_AGENT
     *
     * @param   string  $option  decide whether to set the XML declaration or not
     * @return  string|null  XML declaration or null
     */
    public function getXmlDeclaration($option = null)
    {
        // get xml declaration option from site settings if not given as argument
        if (is_null($option)) {
            $siteSettings = new Model_SiteSettings();
            $option = $siteSettings->get('xml_declaration');
        }
        // return null if content is not XHTML but simply HTML
        if (!$this->view->docType()->isXhtml()) {
            return null;
        }
        // decide whether to return an xml declaration depending on the option
        switch (strtolower($option)) {
            case 'always':
                return '<?xml version="1.0" encoding="' . $this->view->placeholder('charset') . '" ?>' . PHP_EOL;
            case 'browser':
                if ($this->_userAgentAcceptsXhtml()) {
                    return $this->getXmlDeclaration('always');
                }
                return $this->getXmlDeclaration('never');
            case 'never':
            default:
                return null;
        }
    }

    /**
     * Checks whether HTTP User Agent excepts XHTML
     *
     * @return bool
     */
    protected function _userAgentAcceptsXhtml()
    {
        $check_pattern = '|application/xhtml\+xml(?!\s*;\s*q=0)|';
        // Does the UA claim to be able to handle XHTML?
        if (($_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.1')
                && isset($_SERVER['HTTP_ACCEPT'])
                && preg_match($check_pattern, $_SERVER['HTTP_ACCEPT'])) {
            return true;
        }
        // Old Gecko Browsers do have some Crashbugs with XHTML.
        if (isset($_SERVER['HTTP_USER_AGENT'])) {
            if (preg_match("|rv\:0.9|", $_SERVER['HTTP_USER_AGENT'])) {
                return false;
            }
        }
        return false;
    }
}